package com.jhz.speedy.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具
 * Created by JunHeZ on 2017/6/27.
 */

public class DateUtils {
    private static Locale locale = new Locale("zh", "CN");

    public static final String PATTERN_DATE = "yyyy-MM-dd";
    public static final String PATTERN_HH_MM = "HH:mm";
    public static final String PATTERN_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间
     * @param pattern  时间类型
     * @return  时间字符串
     */
    public static String getCurrentDate(String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = format.format(curDate);
        return str;
    }

    /**
     * @param time  毫秒
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatDate(long time) {
        SimpleDateFormat format = new SimpleDateFormat(PATTERN_SS, locale);
        Date curDate = new Date(time);// 获取当前时间
        String str = format.format(curDate);
        return str;
    }

    public static long parseDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat(PATTERN_DATE, locale);
        long time = 0;
        Date date;
        try {
            date = format.parse(str);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
